package com.zkdcloud.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import com.xdja.interceptor.annotation.BeforeProcess;
import com.zkdcloud.aspectInterceptor.advice.MobileTokenAdvice;
import com.zkdcloud.domain.Address;
import com.zkdcloud.domain.User;
import com.zkdcloud.service.AddressService;
import com.zkdcloud.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_USER;

/**
 * @author zk123
 * @version 2018/5/20
 */
@RestController
@RequestMapping(value = "/api")
@BeforeProcess(advice = MobileTokenAdvice.class)
public class MobileAddressController {

    private static Logger logger = LoggerFactory.getLogger(MobileAddressController.class);

    /**
     * 地址service
     */
    @Autowired
    private AddressService addressService;
    /**
     * user service
     */
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addresses/all",method = RequestMethod.GET)
    public Object get(Map<String,Object> map){
        User user = (User) map.get(REQUEST_USER);
        List<Address> addressList = addressService.getByUid(user.getId());
        return addressList;
    }

    /**
     * 更新指定地址
     *
     * @param request request
     * @return obj
     */
    @RequestMapping(value = "/addresses/{id}",method = RequestMethod.PATCH)
    public Object update(@PathVariable String id, HttpServletRequest request) throws IOException {
        Address address = JSON.parseObject(request.getInputStream(), Charset.forName("UTF-8"),Address.class);
        address.setId(id);
        addressService.update(address);
        return addressService.getById(id);
    }

    /**
     * 删除指定地址
     *
     * @param id id
     * @return null obj
     */
    @RequestMapping(value = "/addresses/{id}",method = RequestMethod.DELETE)
    public Object remove(@PathVariable String id){
        addressService.removeById(id);
        return new Object();
    }

    /**
     * 添加user
     *
     * @param request request
     * @param map map
     * @return address
     */
    @RequestMapping(value = "/addresses/add",method = RequestMethod.POST)
    public Object add(HttpServletRequest request,Map<String,Object> map) throws IOException {
        User user = (User) map.get(REQUEST_USER);
        Address address = JSON.parseObject(request.getInputStream(), Charset.forName("UTF-8"),Address.class);
        address.setUserId(user.getId());

        address = addressService.addAddress(address);
        return address;
    }

    /**
     * 设置最近地址
     *
     * @param id uid
     * @param lastAddressId lastAddressId
     * @return user
     */
    @RequestMapping(value = "/address/set/lastAddress/{id}",method = RequestMethod.POST)
    public Object setLastUpdateAddress(@PathVariable String id, String lastAddressId){
        User user = new User();
        user.setId(id);
        user.setLastAddressId(lastAddressId);

        userService.updateUser(user);
        return userService.getById(id);
    }
}
